import { checkYouTube, saveAuthInfo, checkYouTubeAuthor } from './youtubeConfirm.js'
import U from '@/js/utils.js';
import '@/js/message.js';
let isEdit = false, hasConfirm = false, isAuto = 0;
let obj = {
	dialogObj: $("#youTubeAdd"),
	nickNameObj: $(".nickName"),
	serverUrlObj: $(".serverUrl"),
	streamNameObj: $(".streamName"),
	selfSelectObj: $(".selfSelect"),
	inputBoxObj: $(".show_input_box"),
	parentObj: $('.addOrEditIframe', window.parent.document)
};
export default {
	init: function() {
		let _this = this;
		// 判断是否为编辑
		_this.checkIfEdit();
		// 认证
		$(".confirmBtn").on("click", function(e) {
			let nickName = obj.nickNameObj.val();
			if(!U._spaceCheck(nickName)) {
				$.message({message: '输出名称不能为空!', type: 'info'});
				return;
			}
			if(!U._check1_32Num(nickName)) {
				$.message({message: '请输入长度为1到32位的昵称!', type: 'info'});
				return;
			}
			if(nickName.toLowerCase() == null) {
				$.message({message: '输出名称非法!', type: 'info'});
				return;
			}
			_this.toConfirm();
		});
		// 保存
		$(".saveBtn").on("click", function(e) {
			let nickName = obj.nickNameObj.val();
			let serverUrl = obj.serverUrlObj.val();
			let streamName = obj.streamNameObj.val();
			isAuto = U._getV(obj.selfSelectObj, "data-value") == "Automation"? '0' : '1';
			if(!U._spaceCheck(nickName)) {
				$.message({message: '输出名称不能为空!', type: 'info'});
				return;
			}
			if(!U._check1_32Num(nickName)) {
				$.message({message: '请输入长度为1到32位的昵称!', type: 'info'});
				return;
			}
			if(nickName.toLowerCase() == null) {
				$.message({message: '输出名称非法!', type: 'info'});
				return;
			}
			if(isAuto == '1') {
				if(!U._spaceCheck(serverUrl)) {
					$.message({message: 'Server URL不能为空!', type: 'info'});
					return;
				}
				if(!U._spaceCheck(streamName)) {
					$.message({message: 'Stream Name/Key不能为空!', type: 'info'});
					return;
				}
			}
			// 自动模式下
			_this.saveInfo(obj);
			// if(isAuto == "0") {
			// 	$.message({message: '该账号还未进行认证', type: 'info'});
			// 	checkYouTubeAuthor(nickName, function(status) {
			// 		if(!status){
			// 			$.message({message: '该账号还未进行认证', type: 'info'});
			// 		}else{
			// 			_this.saveInfo(obj);
			// 		}
			// 	});
			// }else{
			// 	_this.saveInfo(obj);
			// }
		});
		// 取消及关闭窗口
		$(".quitBtn,.closeBtn").on("click", function(e) {
			isAuto = U._getV(obj.selfSelectObj, "data-value") == "Automation"? '0' : '1';
			if(isAuto == "0" && hasConfirm) {
				$.message({message: '当前账户还未保存，确认关闭？', type: 'info'});
				// return;
			}
			U._closeParent("youtubeAdd");
		});
		// 切换radio
		$(".selfSelect li").on("click", function(e) {
			if(isEdit)return;
			U._changeRadio($(this), "toggleShow");
		});
		// 切换Stream查看状态
		$(".eyeIcon").on("click", function() {
			U._changeInput($(this), $(".streamName"));
		});
	},
	checkIfEdit: function() {
		isEdit = U._getV(obj.parentObj, "operate-type") == "edit"? true: false;
		if(!isEdit)return;
		$(".eyeIcon").removeClass("i-eyeView-show").addClass("i-eyeView-hide");
		U._setV(obj.streamNameObj, "type", "password")
		let rowData = JSON.parse(U._getV(obj.parentObj, "edit-data"));
		if(rowData.type != "Y")return;
		if(rowData.isAuto == "0"){
			U._setV(obj.nickNameObj, "readOnly", "true");
			$(".auto_box").removeClass("hide").siblings(".manual_box").addClass("hide");
			$(".selfSelect li").eq(0).addClass("selected").siblings("li").removeClass("selected");
		}
		if(rowData.isAuto == "1"){
			U._setV(obj.nickNameObj, "readOnly", "false");
			$(".manual_box").removeClass("hide").siblings(".auto_box").addClass("hide");
			$(".selfSelect li").eq(1).addClass("selected").siblings("li").removeClass("selected");
		}
		$(".selfSelect li").addClass("notAllowed");
		obj.nickNameObj.val(rowData.nickName);
		obj.serverUrlObj.val(rowData.token);
		obj.streamNameObj.val(rowData.rtmpId);
		// 自动模式在编辑前需要验证当前用户是否需要重新认证
		if(rowData.isAuto == "0") {
			$(".auto_box,.popupFooter").addClass("hide");
			checkYouTubeAuthor(rowData.nickName, function(status) {
				if(!status){
					$(".auto_box,.editReConfirmTips").removeClass("hide");
				}
			});
		}
	},
	toConfirm: function () {
		let nickName = obj.nickNameObj.val();
		let auto_box = $(".auto_box");
		new checkYouTube(nickName, function(params){
			let paramsObj = JSON.parse(params);
			if(paramsObj) {
				auto_box.addClass("hide");
				hasConfirm = true;
				U._setV(obj.nickNameObj, "readOnly", "true");
			}else{
				hasConfirm = false;
			}
		});
	},
	saveInfo: function() {
		let pwd_1 = '625202f9869e068d';
		let iv_1 = '5efd8f6060e20880';
		let pwd_2 = '1020304050607080';
		let iv_2 = 'a1b2c3d4e5f6g7h8';
		let nickName = obj.nickNameObj.val();
		let serverUrl = obj.serverUrlObj.val() || "";
		let streamName = obj.streamNameObj.val() || "";
		let token = U._getAesString(streamName, pwd_1, iv_1);
		let operateType = U._getV(obj.parentObj, "operate-type");
		isAuto = U._getV(obj.selfSelectObj, "data-value") == "Automation"? '0' : '1';
		let objP;
		if(operateType == "edit") {
			objP = {
				id: U._getV(obj.parentObj, "data-id"),
				nickName: nickName,
				type: "Y",
				token: token,
				rtmpId: serverUrl
			}
		}else{
			objP = {
				nickName: nickName,
				isAuto: isAuto,
				type: "Y",
				token: token,
				rtmpId: serverUrl
			}
		}
		saveAuthInfo(objP, function(status, msg) {
			if(status) {
				if(operateType == "edit") {
					$.message({message: "更新成功！", type: "success"});
				}else{
					$.message({message: "认证成功！", type: "success"});
				}
			}else{
				$.message({message: msg, type: "info"});
			}
			U._closeParent("youtubeAdd");
			parent.upDateSocialList();
		})
	}
}